﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace WebApplication6.manage
{
    public partial class product1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {


            //DropDownList动态绑定

            string sql = "select * from t_fenlei ";

            DataTable dt = SQLHelper.ExecuteDataTable(sql);

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                DataRow row = dt.Rows[i];
                fenlei.Items.Add(new ListItem(Convert.ToString(row["mingchen"]), Convert.ToString(row["mingchen"]) + ""));
            }


            if (!IsPostBack)
            {
                string action = Request["action"];

                if (action == "add")
                {
                    Label1.Text = "添加新商品";
                }
                else if (action == "edit")
                {
                    Label1.Text = "编辑商品";

                    string id = Request["id"];

                    sql = "select * from t_product where id=" + id;

                    dt = SQLHelper.ExecuteDataTable(sql);


                    spname.Text = Convert.ToString(dt.Rows[0]["spname"]);
                    fenlei.Text = Convert.ToString(dt.Rows[0]["fenlei"]);
                    jiage.Text = Convert.ToString(dt.Rows[0]["jiage"]);
                    jianjie.Text = Convert.ToString(dt.Rows[0]["jianjie"]);
                  




                }
                else if (action == "show")
                {
                    Label1.Text = "查看商品";

                    string id = Request["id"];
                    sql = "select * from t_product where id=" + id;

                    dt = SQLHelper.ExecuteDataTable(sql);

                    spname.Text = Convert.ToString(dt.Rows[0]["spname"]);
                    fenlei.Text = Convert.ToString(dt.Rows[0]["fenlei"]);
                    jiage.Text = Convert.ToString(dt.Rows[0]["jiage"]);
                    jianjie.Text = Convert.ToString(dt.Rows[0]["jianjie"]);



                    fenlei.Enabled = false;

                    spname.ReadOnly = true;
                    jiage.ReadOnly = true;
                    jianjie.ReadOnly = true;

                    Button1.Visible = false;

                }
                else if (action == "del")
                {
                    string id = Request["id"];

                    sql = " delete from t_product where id=" + id;

                    SQLHelper.ExecuteNonQuery(sql);

                    Response.Write("<script language=javascript>alert('操作成功');window.location.href='productlist.aspx';</script>");

                }
                else if (action == "tj")
                {

                    string id = Request["id"];

                    sql = "update t_product set tuijian='已推荐'  where id=" + id;


                    SQLHelper.ExecuteNonQuery(sql);

                    

                    Response.Write("<script language=javascript>alert('操作成功');window.location.href='productlist.aspx';</script>");

                }
                else if (action == "qtj")
                {
                    string id = Request["id"];

                    sql = "update t_product set tuijian='未推荐'  where id=" + id;


                    SQLHelper.ExecuteNonQuery(sql);
                  
                    Response.Write("<script language=javascript>alert('操作成功');window.location.href='productlist.aspx';</script>");

                }

                else
                {

                    throw new Exception("action错误" + action);
                }


            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string sql = "";

            if (!IsValid)
            {

                return;
            }

            string action = Request["action"];

            if (action == "add")
            {



                sql = @"insert into t_product (spname,fenlei,jiage,jianjie,tuijian,createtime,dianjishu,xiaoliang) values  ('"
                    + spname.Text + "','" + fenlei.Text + "','" + jiage.Text
                    + "','" + jianjie.Text + "','未推荐','" + Convert.ToString(DateTime.Now) + "',0,0)";


                SQLHelper.ExecuteNonQuery(sql);

                Response.Write("<script language=javascript>alert('操作成功');window.location.href='productlist.aspx';</script>");
            }
            else if (action == "edit")
            {

                string id = Request["id"];



                sql = "update t_product set spname='" + spname.Text + "', fenlei='" + fenlei.Text
                    + "' ,jiage='" + jiage.Text + "' ,jianjie='" + jianjie.Text + "' where id=" + id;


                SQLHelper.ExecuteNonQuery(sql);


                Response.Write("<script language=javascript>alert('操作成功');window.location.href='productlist.aspx';</script>");

            }
            else
            {

                throw new Exception("action错误" + action);
            }

        }
    }
}
